package com.dl7.mvp.music.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.base.BaseFragment;
import com.dl7.mvp.music.bean.MusicCategoryList;
import com.dl7.mvp.mvp.R;
import com.dl7.mvp.mvp.databinding.FragmentMusicMainBinding;
import com.dl7.mvp.base.baseadapter.MyFragmentPagerAdapter;

import java.util.ArrayList;

public class MusicMainFragment extends BaseFragment<FragmentMusicMainBinding> {
    private ArrayList<String> mTitleList = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        loadData();
    }

    @Override
    public int getContentViewResID() {
        return R.layout.fragment_music_main;
    }

    private void initView() {
        //getSupportFragmentManager()是Activity嵌套fragment时使用
        //getChildFragmentManager()是Fragment嵌套Fragment时使用
        FragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitleList);
        bindingView.viewPager.setAdapter(adapter);
        // 左右预加载页面的个数
        bindingView.viewPager.setOffscreenPageLimit(3);
        adapter.notifyDataSetChanged();
        bindingView.tableLayout.setTabMode(TabLayout.MODE_FIXED);
        //设置Viewpager和Tablayout进行联动
        bindingView.tableLayout.setupWithViewPager(bindingView.viewPager);
        // item点击跳转
        // initRxBus();
    }

    private void initFragmentList(ArrayList<MusicCategoryList.DataBean> data) {
        mTitleList.clear();
        mFragments.clear();
//        //初始化导航标题,如果是title在json数据中,在初始化的时候可以使用异步任务加载的形式添加
//        mTitleList.add("推荐");
//        //初始化Fragment
//        mFragments.add(new RcmdMusicFragment());
        if (data != null) {
            for (int i = 0; i < data.size(); i++) {
                MusicCategoryList.DataBean dataBean = data.get(i);
                mTitleList.add(dataBean.getName());
                // (dataBean.getId(), dataBean.getPlaylists());
                mFragments.add(MusicAlbumListFragment.newInstance(dataBean.getId()));
            }
        }
        showQuickControl(true);
    }

    private void loadData() {
        RetrofitService.getRcmdMusicCategoryList()
                .compose(bindToLife())
                .subscribe(categoryList -> {
                    initFragmentList((ArrayList<MusicCategoryList.DataBean>) categoryList);
                    bindingView.viewPager.getAdapter().notifyDataSetChanged();
                });
    }

    private QuickControlsFragment fragment; //底部播放控制栏
    /**
     * @param show 显示或关闭底部播放控制栏
     */
    protected void showQuickControl(boolean show) {
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        if (show) {
            if (fragment == null) {
                fragment = QuickControlsFragment.newInstance();
                ft.add(R.id.bottom_container, fragment).commitAllowingStateLoss();
            } else {
                ft.show(fragment).commitAllowingStateLoss();
            }
        } else {
            if (fragment != null)
                ft.hide(fragment).commitAllowingStateLoss();
        }
    }
}
